# -*- encoding: utf-8 -*-

"""
6.4.2 聚合
"""

import pandas as pd

data = {
    '日期': ['2020-03-11','2020-03-11','2020-03-11','2020-03-11','2020-03-11',
            '2020-03-12','2020-03-12','2020-03-12','2020-03-12','2020-03-12',
            '2020-03-13','2020-03-13','2020-03-13','2020-03-13','2020-03-13'],
    '代码': ['000625.SZ','000762.SZ','600132.SH','600009.SH','000882.SZ',
            '000625.SZ','000762.SZ','600132.SH','600009.SH','000882.SZ',
            '000625.SZ','000762.SZ','600132.SH','600009.SH','000882.SZ'],
    '成交额': [422.08,73.65,207.04,510.59,63.28,
            471.78,59.2,156.82,853.83,52.84,
            789.1,57.01,223.06,1196.14,56.32],
    '成交量': [37091400,9315300,4127800,7233100,28911100,
            42471700,7724200,3143100,12350400,24828900,
            68771048,7741802,4496598,17662768,27484360]
}

vo = pd.DataFrame(data)

print(vo.groupby('日期').sum()) # 按日期统计全部股票的成交总额和成交总量
print(vo.groupby('代码').mean()) # 统计各个股票的多个交易日的平均成交额和平均成交量

def scope(x): # 返回最大值和最小值之差（波动幅度）的函数
    return x.max()-x.min()

print(vo.groupby('代码').agg(scope)) # 统计每一只股票成交额和成交量的波动幅度
print(vo.groupby('代码').agg(['mean', scope])) # 统计成交额和成交量的均值和波动幅度
print(vo.groupby('代码').agg({'成交额':'mean', '成交量':scope})) # 对成交额实施均值操作，对成交量实施自定义的波动幅度函数
